package com.acwing.partition4;

import java.io.*;
import java.util.Arrays;

/**
 * @author `RKC`
 * @date 2022/2/7 15:13
 */
public class AC376机器任务 {

    private static final int N = 110, M = 110;
    private static boolean[][] graph = new boolean[N][M];
    private static int[] match = new int[N];
    private static boolean[] st = new boolean[N];
    private static int n = 0, m = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        for (;;) {
            String[] ss = reader.readLine().split(" ");
            n = Integer.parseInt(ss[0]);
            if (n == 0) break;
            for (int i = 0; i < N; i++) {
                Arrays.fill(graph[i], false);
                match[i] = -1;
            }
            m = Integer.parseInt(ss[1]);
            int k = Integer.parseInt(ss[2]);
            for (int i = 0; i < k; i++) {
                ss = reader.readLine().split(" ");
                int t = Integer.parseInt(ss[0]), a = Integer.parseInt(ss[1]), b = Integer.parseInt(ss[2]);
                if (a == 0 || b == 0) continue;
                graph[a][b] = true;
            }
            int ans = 0;
            for (int i = 0; i < n; i++) {
                Arrays.fill(st, false);
                if (dfs(i)) ans++;
            }
            writer.write(ans + "\n");
        }
        writer.flush();
    }

    private static boolean dfs(int u) {
        for (int i = 0; i < m; i++) {
            if (!st[i] && graph[u][i]) {
                st[i] = true;
                if (match[i] == -1 || dfs(match[i])) {
                    match[i] = u;
                    return true;
                }
            }
        }
        return false;
    }
}
